package com.sdcqjy.property.lawRegulations;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.PublicDialog;
import com.sdcqjy.mylibrary.widget.ShowToast;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.BookMarkAdapter;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.mode.LawRegulationsDetailsMode;
import com.sdcqjy.property.presenter.LawRegulationsDetailsPresenter;
import com.sdcqjy.property.presenter.contract.LawRegulationsDetailsContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 磁磁帅 on 2018/7/8.
 * 功能：
 */

public class BookMarkFragment extends BaseFragment<LawRegulationsDetailsPresenter> implements
        LawRegulationsDetailsContract.View, OnRefreshListener, BookMarkAdapter.OnAdapterClickListener {
    @BindView(R.id.listView)
    RecyclerView listView;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;
    private List<LawRegulationsDetailsMode> modeList;
    private BookMarkAdapter adapter;

    public static BookMarkFragment newInstance() {
        return new BookMarkFragment();
    }

    public BookMarkFragment() {
        presenter = new LawRegulationsDetailsPresenter(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_mark, null);
        ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        modeList = new ArrayList<>();
        adapter = new BookMarkAdapter(getActivity(), modeList)
                .setOnAdapterClickListener(this);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listView.addItemDecoration(new SpacesItemDecoration(OtherUtils.dip2px(getActivity(), 1)
                , new boolean[]{false, false, false, true}));

        smartLayout.setOnRefreshListener(this);

        smartLayout.autoRefresh();
    }

    public void onDestroyView() {
        super.onDestroyView();
    }

    public void onRefresh(RefreshLayout refreshlayout) {
        if (isLogin()) presenter.getBookMark_();
    }

    public void applyError(String t) {
        super.applyError(t);
        smartLayout.finishRefresh();
    }

    public void addBookMarkSucc() {
    }

    public void getBookMarkRet(List<LawRegulationsDetailsMode> list) {
        smartLayout.finishRefresh();
        if (list == null) list = new ArrayList<>();
        modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void delBookMarkSucc() {
        ShowToast.alertShortOfWhite(getActivity(), R.string.sanchuchenggong);
        if (deleBookMarkMode != null) {
            modeList.remove(deleBookMarkMode);
            adapter.notifyDataSetChanged();
            deleBookMarkMode = null;
        }
    }

    private LawRegulationsDetailsMode deleBookMarkMode;

    public void deleBookMark(LawRegulationsDetailsMode mode) {
        deleBookMarkMode = mode;
        PublicDialog dialog = new PublicDialog(getActivity());
        dialog.setTitle(null);
        dialog.setContent(R.string.querensanchu);
        dialog.setOnPublicDialogClick(new PublicDialog.OnPublicDialogClick() {
            public void onConfirmClick() {
                if (isLogin()) presenter.delBookMark(deleBookMarkMode);
            }

            public void onCancelClick() {
            }
        });
        dialog.show();
    }

    public void toBookMark(LawRegulationsDetailsMode mode) {
        mode.id = mode.lawID;
        LawRegulationsDetailsActivity.open(getActivity(), mode);
    }
}